/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import databaseclient.DatabaseClientUI;
import databaseinterface.DatabaseInterface;
import java.net.InetAddress;
import java.rmi.Naming;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author tinvukhac
 */
public class AccountServices {

    public boolean login(JTextField txtUserId, JPasswordField txtPassword, String serverIp,int port) {
        boolean invalid = false;
        String userId;
        String password;
        String ipAddress;

        // Get information from controls
        userId = txtUserId.getText().trim();
        password = new String(txtPassword.getPassword());

        // Validate
        if ((userId.length() == 0) || (password.length() == 0)) {
            invalid = true;
        } else {
            try {
                String message;
                // Get IP Address
                InetAddress thisIp = InetAddress.getLocalHost();
                ipAddress = thisIp.getHostAddress();

                databaseinterface.DatabaseInterface dataitf = null;
                dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
                message = dataitf.checkLoginUser(userId, password, ipAddress,port);
                if (message.equals("ok")) {
                    DatabaseClientUI clientUI = new DatabaseClientUI(userId, serverIp,port);
                    clientUI.setVisible(true);

                    return true;
                } else if (message.equals("account is loged in")) {
                    JOptionPane.showMessageDialog(null, message, "account is loged in", 0);
                } else if (message.equals("username or password is incorrect")) {
                    JOptionPane.showMessageDialog(null, message, "username or password is incorrect", 0);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Exception : " + e.toString(), "Exception", 0);
            }
        }
        return false;
    }

    public boolean register(JTextField txtUserId, JPasswordField txtPassword,
            JPasswordField txtPasswordConfirm, String serverIp,int port) {
        boolean invalid = false;
        String userId;
        String password;
        String passwordConfirm;
        String ipAddress;

        // Get information from controls
        userId = txtUserId.getText().trim();
        password = new String(txtPassword.getPassword());
        passwordConfirm = new String(txtPasswordConfirm.getPassword());

        // Validate
        if ((userId.length() == 0) || (password.length() == 0) || (passwordConfirm.length() == 0)) {
            invalid = true;
        } else {
            if (password.equals((String) passwordConfirm)) {
                // TODO add your handling code here:
                try {
                    String message;
                    // Get IP Address
                    InetAddress thisIp = InetAddress.getLocalHost();
                    ipAddress = thisIp.getHostAddress();

                    databaseinterface.DatabaseInterface dataitf = null;
                    dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
                    message = dataitf.insertNewUser(userId, password, ipAddress,port);
                    if (message.equals("OK")) {
                        JOptionPane.showMessageDialog(null, "Register Successful!", "Message", 1);
                        DatabaseClientUI clientUI = new DatabaseClientUI(userId, serverIp, port);
                        clientUI.setVisible(true);

                        return true;
                    } else {
                        JOptionPane.showMessageDialog(null, message, "Exception", 0);
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Exception : " + e.toString(), "Exception", 0);
                }
            } else {
                invalid = true;
            }
        }
        if (invalid) {
            JOptionPane.showMessageDialog(null, "Username or password is incorrect", "Invalid", 0);
        }

        return false;
    }

    public void confirm(String userId, String serverIp) {
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            dataitf.confirm(userId);
        } catch (Exception e) {
        }
    }
    
    public int getNumberSharing(int fileId,String serverIp){
        int number=-1;
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            number=dataitf.getNumberSharing(fileId);
        } catch (Exception e) {
            System.out.println("getNumberSharing in AccountServices");
            e.printStackTrace();
        }
        return number;
    }
    
    public int getNumberDownloading(int fileId,String serverIp){
        int number=-1;
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            number=dataitf.getNumberDownloading(fileId);
        } catch (Exception e) {
            System.out.println("getNumberDownloading in AccountServices");
            e.printStackTrace();
        }
        return number;
    }
}
